package com.base.cn.platform.os.controller.manage.count;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.entity.manage.count.PageViewCountNum;
import com.base.cn.platform.os.service.manage.count.PageUVCountBiz;
import com.base.cn.platform.os.service.manage.count.PageViewCountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 页面浏览量(PV)统计Controller
 *
 * @author s.li
 * @create 2018-08-23-20:22
 */
@RestController
@RequestMapping("/manage")
public class PageViewCountController extends BaseController {
    @Autowired
    private PageViewCountBiz pageViewCountBiz;
    @Autowired
    private PageUVCountBiz pageUVCountBiz;

    /**
     * 查询每个栏目访问量统计数据
     * @return 统计数据实体
     */
    @RequestMapping("/queryPageViewCountNum")
    public List<PageViewCountNum> queryPageViewCountNum(){
        List<PageViewCountNum> pageViewCountNumList = pageViewCountBiz.queryPageViewCountNum();
        return pageViewCountNumList;
    }

    /**
     * PC平台总PV数、PC平台总UV数
     * @return Map<String,Integer>
     */
    @RequestMapping("/queryPageUVpvSumNum")
    public Map<String,Integer> queryPageUVpvSumNum(@RequestBody Map<String,String> map){
        StringBuilder whereSql =  new StringBuilder(" 1=1 ");
        if(StringUtils.isNotEmpty(map.get("begTime"))){
            whereSql.append(" and DATE_FORMAT(createTime,'%Y-%m-%d') >=DATE_FORMAT( '"+map.get("begTime")+"','%Y-%m-%d')");
        }
        if(StringUtils.isNotEmpty(map.get("endTime"))){
            whereSql.append(" and DATE_FORMAT(createTime,'%Y-%m-%d') <=DATE_FORMAT( '"+map.get("endTime")+"','%Y-%m-%d')");
        }
        Map<String,Integer> pageUVpvNumMap = new HashMap<>();
        BigDecimal uvCountNum = pageUVCountBiz.count(whereSql.toString());
        BigDecimal pvCountNum = pageViewCountBiz.count(whereSql.toString());
        pageUVpvNumMap.put("uvCountNum",uvCountNum.intValue());
        pageUVpvNumMap.put("pvCountNum",pvCountNum.intValue());
        return pageUVpvNumMap;
    }

    /**
     * 添加PV、UV统计数据
     * @param request  HttpServletRequest
     * @param pageKey 页面关键字
     * @param fromSource 访问来源
     */
    @RequestMapping("/addPageCount")
    public void addPageCount(HttpServletRequest request,
                             @RequestParam("pageKey") String pageKey,
                             @RequestParam("fromSource") String fromSource){
        String ip = WebUtils.getIpAddr(request);
        pageUVCountBiz.addPageCount(ip,pageKey,fromSource);
    }
}
